﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;

namespace SweetSurvey.Tests.RepositoryTests
{
    /// <summary>
    ///This is a test class for QuestionRepositoryTest and is intended
    ///to contain all QuestionRepositoryTest Unit Tests
    ///</summary>
    [TestClass]
    public class QuestionRepositoryTest : RepositoryTestBase
    {
        #region Create Sample Data

        public static string CreateSampleQuestion(string surveyId = null, short index = 0)
        {
            if (string.IsNullOrEmpty(surveyId))
                surveyId = SurveyRepositoryTest.CreateSampleSurvey();

            return (new QuestionRepository()).Create(new Question()
            {
                SurveyId = surveyId,
                Title = "Sample Question Title",
                Index = index
            });
        }

        public static IList<string> CreateSampleQuestions(string surveyId, short count = 10)
        {
            List<string> ids = new List<string>();
            for (short i = 0; i < count; i++)
            {
                ids.Add(CreateSampleQuestion(surveyId, i));
            }
            return ids;
        }

        #endregion

        /// <summary>
        ///A test for GetSampleQuestions
        ///</summary>
        [TestMethod]
        public void GetSampleQuestionsTest()
        {
            // prepare
            var r = new QuestionRepository();
            
            // act
            var questions = r.GetSampleQuestions();

            // assert
            Assert.IsTrue(questions.Count() > 0);
            Assert.IsTrue(questions.All(q => q.Options.Count > 0));
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod]
        public void UpdateTest()
        {
            // variables
            var questionCount = 20;
            var newTitle = "Sample Question Title (Updated)";

            // prepare
            var surveyId = SurveyRepositoryTest.CreateSampleSurvey();
            var questionIds = CreateSampleQuestions(surveyId, (short)questionCount);
            var repository = new QuestionRepository();

            // act
            foreach (var questionId in questionIds)
            {
                var q = repository.Get(questionId);
                q.Title = newTitle;
                repository.Update(q);
            }

            // assert
            Assert.AreEqual(questionCount, questionIds.Count);
            Assert.IsTrue(questionIds.All(id => repository.Get(id).Title == newTitle));
        }

        /// <summary>
        ///A test for ValidateSequence
        ///</summary>
        [TestMethod]
        public void ValidateSequenceTest()
        {
            // prepare
            var surveyId = SurveyRepositoryTest.CreateSampleSurvey();
            var questionIds = CreateSampleQuestions(surveyId);
            var repository = new QuestionRepository();
            
            // act
            var result_original = repository.ValidateSequence(surveyId, questionIds);
            questionIds.Insert(1, RepositoryBase.GenerateRandomId());
            var result_afterInsertion = repository.ValidateSequence(surveyId, questionIds);
            questionIds.RemoveAt(1);
            string temp = questionIds[5];
            questionIds[5] = questionIds[6];
            questionIds[6] = temp;
            var result_afterSwap = repository.ValidateSequence(surveyId, questionIds);

            // assert
            Assert.IsTrue(result_original);
            Assert.IsFalse(result_afterInsertion);
            Assert.IsFalse(result_afterSwap);
        }
    }
}
